package com.best.onegous;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import org.json.JSONException;
import org.json.JSONObject;

import com.readystatesoftware.systembartint.SystemBarTintManager;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PorterDuff.Mode;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;


public class MainActivity extends BaseActivity implements OnClickListener{

	FragmentManager fManager;
	private ImageView homeimg, searchimg, leshanghaiimg, myimg,shoppingimg;
	private Home home;
	private Periphery search;
	private Leshanghai leshanghai;
	private Shopping shopping;
	private My my;
	//private SystemBarTintManager tintManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		setThem(R.layout.activity_main);

	/*	tintManager = new SystemBarTintManager(this);
		  tintManager.setStatusBarTintEnabled(true);
	         tintManager.setNavigationBarTintEnabled(true);
	         tintManager.setNavigationBarAlpha(1);
	         tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
	         
	         tintManager.setStatusBarTintColor(Color.parseColor("#E9CDA6"));
        */
		fManager = getSupportFragmentManager();
		FragmentTransaction transaction = fManager.beginTransaction();
		home = new Home();
		//home.setitem(this);
		transaction.add(R.id.content, home);
		transaction.commit();
		initView();
		
	}


	

	private void initView() {
		
		homeimg = (ImageView) findViewById(R.id.one);
		searchimg = (ImageView) findViewById(R.id.two);
		leshanghaiimg = (ImageView) findViewById(R.id.three);
		shoppingimg = (ImageView) findViewById(R.id.four);
		myimg = (ImageView) findViewById(R.id.five);

		homeimg.setOnClickListener(this);
		searchimg.setOnClickListener(this);
		leshanghaiimg.setOnClickListener(this);
		shoppingimg.setOnClickListener(this);
		myimg.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		//toast("1111");
	}

	// 获取屏幕宽度
	public int getDisplayMetricsWidth() {
		int i = this.getWindowManager().getDefaultDisplay().getWidth();
		int j = this.getWindowManager().getDefaultDisplay().getHeight();
		return Math.min(i, j);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.one:
			setChioceItem(0);
			/*tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
			 tintManager.setTintColor(Color.parseColor("#E9CDA6"));*/
			break;
		case R.id.two:
			setChioceItem(1);
		/*	tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
			 tintManager.setTintColor(Color.parseColor("#E9CDA6"));*/
			break;
		case R.id.three:
			setChioceItem(2);
			/*tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
			 tintManager.setTintColor(Color.parseColor("#E9CDA6"));*/
			break;
		case R.id.four:
			setChioceItem(3);
		/*	tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
			 tintManager.setTintColor(Color.parseColor("#E9CDA6"));*/
			break;
		case R.id.five:
			setChioceItem(4);
			/*tintManager.setNavigationBarTintColor(Color.parseColor("#00000000"));
			 tintManager.setTintColor(Color.parseColor("#00000000"));//3f000000
*/			break;

		default:
			break;
		}
	}

	public void setChioceItem(int index) {
		FragmentTransaction transaction = fManager.beginTransaction();
		clearChioce();
		hideFragments(transaction);
		switch (index) {
		case 0:
			homeimg.setImageResource(R.drawable.s2);
			if (home == null) {
				home = new Home();
				transaction.add(R.id.content, home);
			} else {
				transaction.show(home);
			}
			break;
		case 1:
			searchimg.setImageResource(R.drawable.z2);
			if (search == null) {
				search = new Periphery();
				transaction.add(R.id.content, search);
			} else {
				transaction.show(search);
			}
			break;
		case 2:
			leshanghaiimg.setImageResource(R.drawable.q2);
			if (leshanghai == null) {
				leshanghai = new Leshanghai();
				transaction.add(R.id.content, leshanghai);
			} else {
				transaction.show(leshanghai);
			}
			break;
		case 3:
			shoppingimg.setImageResource(R.drawable.d2);
			if (shopping == null) {
				shopping = new Shopping();
				transaction.add(R.id.content, shopping);
			} else {
				transaction.show(shopping);
			}
		
			break;
		case 4:
			myimg.setImageResource(R.drawable.w2);
			if (my == null) {
				my = new My();
				transaction.add(R.id.content, my);
			} else {
				transaction.show(my);
			}
			break;

		default:
			break;
		}
		transaction.commit();
	}

	private void hideFragments(FragmentTransaction transaction) {
		if (home != null) {
			transaction.hide(home);
		}
		if (search != null) {
			transaction.hide(search);
		}
		if (leshanghai != null) {
			transaction.hide(leshanghai);
		}
		if (shopping != null) {
			transaction.hide(shopping);
		}
		if (my != null) {
			transaction.hide(my);
		}
	}

	public void clearChioce() {
		homeimg.setImageResource(R.drawable.s1);
		searchimg.setImageResource(R.drawable.z1);
		leshanghaiimg.setImageResource(R.drawable.q1);
		shoppingimg.setImageResource(R.drawable.d1);
		myimg.setImageResource(R.drawable.w1);

	}



	/**
	 * 菜单、返回键响应
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	 // TODO Auto-generated method stub
	 if(keyCode == KeyEvent.KEYCODE_BACK)
		{ 
		  exitBy2Click(); //调用双击退出函数
		}
	 return false;
	}
	/**
	 * 双击退出函数
	 */
	private static Boolean isExit = false;
	 
	private void exitBy2Click() {
	 Timer tExit = null;
	 if (isExit == false) {
		 isExit = true; // 准备退出
		 tExit = new Timer();
		 tExit.schedule(new TimerTask() {
		  @Override
		  public void run() {
		  isExit = false; // 取消退出
		  
		  }
		  
	  }, 2000); // 如果2秒钟内没有按下返回键，则启动定时器取消掉刚才执行的任务
		 toast("再按一次退出！");
	 } else {
			 finishAll();
	 }
	}






}
